﻿using ServerCrafter.ClassLibrary;
using ServerCrafter.ClassLibrary.ViewModels;
using System;
using System.Windows;
using Telerik.Windows.Controls;

namespace ServerCrafter.WPF
{
    /// <summary>
    /// Interaction logic for ServerCrafter.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private RadRadioButton checkedUiColorsButton;

        #region Initialization

        public MainWindow()
        {
            PreInitialization();
            InitializeComponent();
            PostInitialization();
        }

        private static void PreInitialization()
        {
            Logger.WriteInitialization();

            Settings.Load.All();

            PropertiesManager.Initialize();
            PropertiesManager.Get();

            Info.System.GetIpAddress();
            Info.System.GetPortStatus();
            Info.System.GetTotalRamInMB();
            Info.System.GetTotalRamInKb();
        }

        private void PostInitialization()
        {
            ServerOutputHandler.Initialize(txtboxOutput);

            // Gets and displays the server properites
            var vmServerProperties = new VMProperties();
            vmServerProperties.Get();
            comboboxProperties.ItemsSource = vmServerProperties.Properties;

            // Gets and adds server commands to autocomplete
            var vmServerCommands = new VMCommands();
            vmServerCommands.Get();
            txtboxCommand.ItemsSource = vmServerCommands.Get();

            // Gets and displays the plugins list
            PluginManager.Plugins.CreateList();
            var vmPlugins = new VmPlugins();
            gridviewPlugins.ItemsSource = vmPlugins.Get();

            // Gets and displays the installed plugins list
            PluginManager.InstalledPlugins.CreateList();
            var installedPlugins = new VMInstalledPlugins();
            gridviewInstalledPlugins.ItemsSource = installedPlugins.Get();

            // Gets and displays the installed mods list
            ModManager.InstalledMods.CreateList();
            var installedMods = new VMInstalledMods();
            gridviewInstalledMods.ItemsSource = installedMods.Get();

            // Gets and displays the installed core mods list
            ModManager.InstalledCoreMods.CreateList();
            var installedCoreMods = new VMInstalledCoreMods();
            gridviewInstalledCoreMods.ItemsSource = installedCoreMods.Get();

            InitializeControlsData();
            //InitializeTimers();
        }

        private void InitializeControlsData()
        {
            txtboxInitialRamAllocation.Text = Convert.ToString(Settings.ServerStartInfo.InitialRam);
            txtboxMaximumRamAllocation.Text = Convert.ToString(Settings.ServerStartInfo.MaximumRam);
        }

        private void InitializeTimers()
        {
            var timerSeconds = new System.Windows.Threading.DispatcherTimer();
            timerSeconds.Tick += timerSeconds_Tick;
            timerSeconds.Interval = new TimeSpan(0, 0, 0, 1, 200);
            timerSeconds.Start();

            var timerMinutes = new System.Windows.Threading.DispatcherTimer();
            timerMinutes.Tick += timerMinutes_Tick;
            timerMinutes.Interval = new TimeSpan(0, 1, 0);
            timerMinutes.Start();
        }

        #endregion Initialization

        #region Timers

        private void timerSeconds_Tick(object sender, EventArgs e)
        {
            // Gets the port number that is in the server.properties file and displays it.
            lblPortNumber.Content = Info.Server.GetPortNumber();

            // Gets the CPU usage of ServerCrafter, the server and the computer and dislays it.
            progbarCpuServerCrafter.Value = Info.System.PerformanceCounters.CPU.ServerCrafterPercentage();
            progbarCpuServer.Value = Info.System.PerformanceCounters.CPU.ServerPercentage();
            progbarCpuComputer.Value = Info.System.PerformanceCounters.CPU.ComputerPercentage();

            // Gets the RAM usage of ServerCrafter, the server and the computer and dislays it.
            progbarRamServerCrafter.Value = Info.System.PerformanceCounters.Ram.ServerCrafterPercentage();
            progbarRamServer.Value = Info.System.PerformanceCounters.Ram.ServerPercentage();
            progbarRamComputer.Value = Info.System.PerformanceCounters.Ram.ComputerPercentage();

            // Displays the server port status.
            lblPortOpen.Content = Info.System.IsPortOpen ? "Yes" : "No";

            // Displays the external IP address.
            lblIpAddress.Content = Info.System.IpAddress;

            // Displays an indicator that indicates whether the server is running or not.
            lblServerStatus.Content = Server.IsRunning ? "Server is running." : "Server is not running.";

            // Gets the server uptime and displays it
            //if (Server.IsRunning) lblServerUptime.Content = Server.Uptime().ToString("h hours m minutes s seconds");
        }

        private static void timerMinutes_Tick(object sender, EventArgs e)
        {
            Info.System.GetIpAddress();
            Info.System.GetPortStatus();
        }

        #endregion Timers
    }
}